package manager.cms.service.exec;

import com.google.gson.Gson;
import common.SnowflakeIdWorker;
import manager.cms.service.JdExecutor;
import manager.cms.service.utils.JdInternetWormUtils;
import manager.feign.ItemJdThirdService;
import manager.spec.group.dao.SpecGroupDao;
import manager.spec.group.dao.SpecificationDao;
import manager.spec.group.vo.SpecGroupVO;
import manager.spec.group.vo.SpecificationVO;
import manager.spec.params.dao.ParamDao;
import manager.spec.params.vo.SpecParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 获取规格参数和规格参数组,设置规格参数分类模板
 */
@Service(value = "spuParamsTempleteExec")
public class SpuParamsTempleteExecImpl implements JdExecutor {
    @Autowired
    private SpecificationDao specificationDao;
    @Autowired
    private ParamDao paramDao;
    @Autowired
    private SpecGroupDao specGroupDao;
    @Autowired
    private ItemJdThirdService itemJdThirdService;
    @Override
    public void execute(Map<String, Object> mapParams) {
             //设置规格参数模板
             String skuId=mapParams.get("skuId").toString();
             String itemHtml=itemJdThirdService.getDetial(skuId);
             Map<String,Object> paramsMap= JdInternetWormUtils.getTemplete(itemHtml);
             SpecificationVO specificationVO=new SpecificationVO();
             String categoryId=(String)mapParams.get("categoryId");
             List<SpecGroupVO> groupVOList=new ArrayList<SpecGroupVO>();
             Map<String,Object> params=(Map<String,Object>)paramsMap.get("paramsTemp");
             List<Map<String,Object>> groupParams=(List<Map<String,Object>>)params.get("groupParams");
             List<SpecParamVO> paramsList=new ArrayList<>();
            for (int i = 0; i < groupParams.size(); i++) {
                Map<String,Object> item= groupParams.get(i);
                String groupId=String.valueOf(SnowflakeIdWorker.nextId());
                SpecGroupVO specGroupVO=new SpecGroupVO();
                specGroupVO.setId(groupId);
                specGroupVO.setCid(categoryId);
                specGroupVO.setName((String)item.get("group"));
                groupVOList.add(specGroupVO);
                //设置规格参数
                List<Map<String,Object>> paramsMapList=(List<Map<String,Object>>)item.get("params");
                List<SpecParamVO> paramsVOList=converParams(paramsMapList,categoryId,groupId,specGroupVO.getName());
                paramsList.addAll(paramsVOList);
            }
             List<Map<String,Object>> paramsTemp=(List<Map<String,Object>>)params.get("params");
             List<SpecParamVO> paramsVOList=converParams(paramsTemp,categoryId,null,null);
             paramsList.addAll(paramsVOList);
             paramsList=converSpecialParams(paramsList,mapParams);
             if(groupVOList.size()>0){
                 specGroupDao.insertList(groupVOList);
             }
             if(paramsList.size()>0){
                 paramDao.insertList(paramsList);
             }
             //存入规格参数模板
             specificationVO.setCategoryId(categoryId);
             specificationVO.setSpecifications((String)paramsMap.get("paramsTempStr"));
             specificationDao.deleteByCondition(specificationVO);
             specificationDao.add(specificationVO);
    }
    private List<SpecParamVO> converParams(List<Map<String,Object>> paramsMapList,String categoryId,String groupId,String grounName){
            List<SpecParamVO> paramsVOList=new ArrayList<>();
            paramsMapList.forEach(curParam->{
                String paramsId=String.valueOf(SnowflakeIdWorker.nextId());
                SpecParamVO specParamVO=new SpecParamVO();
                specParamVO.setId(paramsId);
                specParamVO.setCid(categoryId);
                specParamVO.setGroupId(groupId);
                specParamVO.setName((String)curParam.get("k"));
                String paramsTempStr=new Gson().toJson(curParam.get("options"));
                if(curParam.get("options")!=null){
                    specParamVO.setOptions(paramsTempStr);
                }
                specParamVO.setGroupName(grounName);
                specParamVO.setGeneric(true);
                specParamVO.setNumberic(false);
                specParamVO.setSearching(false);
                specParamVO.setSegments("");
                specParamVO.setUnit("");
                //如果有选项，就不是常规规格参数
                paramsVOList.add(specParamVO);
            });
            return paramsVOList;
    }
    private List<SpecParamVO> converSpecialParams(List<SpecParamVO> paramsMapList,Map<String, Object> mapParams){
        Map<String,Object> conditionMap=(Map<String, Object>) mapParams.get("searchConditionMap");
        List<Map<String,Object>> listParams= (List<Map<String,Object>>)conditionMap.get("searchCondition");
        List<SpecParamVO> paramsVOList=new ArrayList<>();
        paramsMapList.forEach(curParam->{
            List<Map<String,Object>> filterResult=listParams.stream().filter(item->item.get("key").equals(curParam.getName())).collect(Collectors.toList());
            //如果有选项，就不是常规规格参数
            if(null!=curParam.getOptions()){
                 curParam.setGeneric(false);
            }else{
                 curParam.setOptions("");
            }
            if(filterResult.size()>0){
                Map<String,Object> sameItem=filterResult.get(0);
                List<String> segments=(List<String>)sameItem.get("content");
                if(segments.size()>0){
                    String segmentItem=segments.get(1);
                    String[] segmentItemStrs=segmentItem.split("-");
                    if(segmentItemStrs.length==2){
                        Map<String,Object> numberSingleParamsMap=JdInternetWormUtils.getNumberFromStr(segmentItem);
                        List<String> curStrUnit=(List<String>)numberSingleParamsMap.get("listStr");
                        String unit=curStrUnit.get(curStrUnit.size()-1);
                        //长度为2代表有区间,也代表是数字
                        curParam.setUnit(unit);
                        curParam.setNumberic(true);
                        //设置每一个参数的可选区间
                        String segmentsStr="";
                        for (int i = 0; i <segments.size(); i++) {
                              Map<String,Object> numberParamsMap=JdInternetWormUtils.getNumberFromStr(segments.get(i));
                              List<String> curStr=(List<String>)numberParamsMap.get("listNumber");
                              String cruItemStr="";
                             for (int j = 0; j < curStr.size(); j++) {
                                  cruItemStr=cruItemStr+"-"+curStr.get(j);
                             }
                            cruItemStr=cruItemStr.substring(1,cruItemStr.length());
                            segmentsStr=segmentsStr+";"+cruItemStr;
                        }
                        segmentsStr=segmentsStr.substring(1,segmentsStr.length());
                        curParam.setSegments(segmentsStr);
                    }else{
                        String segmentsStr="";
                        for (int i = 0; i <segments.size(); i++) {
                            segmentsStr=segmentsStr+";"+segments.get(i);
                        }
                        segmentsStr=segmentsStr.substring(1,segmentsStr.length());
                        curParam.setSegments(segmentsStr);
                    }
                }
                curParam.setSearching(true);
            }
            if(curParam.getName().equals("品牌")){
                curParam.setSearching(true);
            }
            paramsVOList.add(curParam);
        });
        return paramsVOList;
    }
}
